import { observer } from "../data/index.js";
import { isPlainObject } from "../reactive/utils.js";

Object.prototype.payload = 'qa payload';

console.log(observer);

const convert = (target) => {
  return isPlainObject(target) ? reactive(target) : target;
}

const reactive = (observer) => {
  return new Proxy(observer, {
    get (target, propKey, receiver) {
      if ('__v_isReactive' === propKey) return true;
      // 拦截监听
      // traget(target, 'get', propKey); // 收集依赖
      
      return convert(Reflect.get(target, propKey, receiver));
    },
    set (target, propKey, value, receiver) {
      let isChanged = false;
      const oldVal = Reflect.get(target, propKey, receiver);

      if (oldVal !== value) {
        isChanged = Reflect.set(target, propKey, value, receiver);

        // 触发监听
        // trigger(target, 'set', propKey); // 收集依赖
      }

      return isChanged;
    },
    deleteProperty (target, propKey) {
      const hasOwn = Reflect.getOwnPropertyDescriptor(target, propKey);
      const isDeleted = Reflect.deleteProperty(target, propKey);

      if (hasOwn && isDeleted) {
        // 触发监听
        // trigger(target, 'delete', propKey);
      }
      return isDeleted;
    }
  })
}

const qa = reactive(observer)

console.log(qa);
// console.log(qa.data);
console.log(qa.data.avatar_url);

delete qa.data;
console.log(qa.payload);
console.log(qa);